﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using Microhard.Permission.Interface;
using Microhard.Permission.Models;
using System.Linq;
using Microhard.Core.DBUtility;
using Microhard.Core;

namespace Microhard.Permission_MembershipImpl
{
    /// <summary>
    /// 操作
    /// 刘杰 2012-11-3
    /// </summary>
    public class OperationImpl : IOperation
    {
        public ExecutedResult Add(Operation model)
        {
            ExecutedResult rtn = new ExecutedResult();
            if (model == null)
            {
                rtn.ErrorMsg.Add("OperationAddFailed", "添加操作失败，请联系系统管理员");
                return rtn;
            }
            try
            {
                MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
                MicroHard_Operation op = model.ToEntity();
                op.OperationID = Guid.NewGuid();

                db.MicroHard_Operations.InsertOnSubmit(op);
                db.SubmitChanges();

                rtn.Scope.IsSysManage = true;
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("OperationAddFailed", "添加操作失败，请联系系统管理员");
                Log.Error("OperationAddFailed",ex);
            }
            return rtn;
        }

        public ExecutedResult Update(Operation model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var list = db.MicroHard_Operations.SingleOrDefault(n => n.OperationID == model.OperationID);

            if (list != null)
            {
                list.KeyName = model.KeyName;
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    rtn.ErrorMsg.Add("OperationUpdateFailed", "修改操作失败，请联系系统管理员");
                    Log.Error("OperationUpdateFailed", ex);
                }

            }

            return rtn;
        }

        public ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var operations = from operation in db.MicroHard_Operations
                             where ids.Contains(operation.OperationID)
                             select operation;
            if (operations.Count() > 0)
            {
                foreach (var delOper in operations)
                {
                    //是否被使用
                    if (delOper.MicroHard_Permissions != null && delOper.MicroHard_Permissions.Count > 0)
                    {
                        rtn.ErrorMsg.Add("OperationImpl.Delete", "操作:" + delOper.Name + "正在使用中，无法删除");
                        continue;
                    }
                    db.MicroHard_Operations.DeleteOnSubmit(delOper);
                }
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    rtn.ErrorMsg.Add("OperationDeleteFailed", "删除操作失败，请联系系统管理员");
                    Log.Error("OperationDeleteFailed", ex);
                }
            }
            return rtn;
        }

        public Operation GetModelByID(Guid id)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var list = db.MicroHard_Operations.SingleOrDefault(n => n.OperationID == id);

            if (list != null)
            {
                Operation op = list.ToModel();
                op.OperationID = id;
                op.Name = op.Name;
                return op;
            }
            return null;
        }

        public List<Operation> Query(Operation Model, int pageIndex, int pageSize, out int totalCount)
        {
            List<Operation> rtn = new List<Operation>();
            var db = new MicrohardOUGDBDataContext();
            IEnumerable<MicroHard_Operation> data = null;
            if (Model.Name == null)
            {
                //_DB,是实例化Linq类                
                data = from _Data in db.MicroHard_Operations
                             select _Data;
            }
            else
            {
                data = db.MicroHard_Operations.Where(n => n.Name == Model.Name);

            }

            //数据总记录数  
            totalCount = data.Count();
            rtn = data.Take(pageSize).Skip((pageIndex - 1) * pageSize).Select(p=>p.ToModel()).ToList();
            return rtn;  
        }

        public List<Operation> Query(Operation Model)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            return db.MicroHard_Operations.Select(o => o.ToModel()).ToList();
        }

        public ExecutedResult Delete(Operation models)
        {
            throw new NotImplementedException();
        }


        #region IRepository<Operation> Members


        public Operation GetModelByModel(Operation model)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IRepository<Operation> Members


        public ScopeResult scope
        {
            get;
            set;
        }

        #endregion
    }
}
